/*
 * TITLE:RequestToServerTable
 * AUTHOR:Manori Thakur
 * PURPOSE: Maintains a list of request codes and their respective classes for instantiation
 */
package clientcore;

import java.util.HashMap;

import android.util.Log;
import clientrequest.*;

public class RequestToServerTable {
	private static final String TAG = RequestToServerTable.class.getSimpleName();
    private static HashMap<Short, Class> requestTable = null;

    public RequestToServerTable() {

    }

    public static void init(AndroidMain ac) {
        requestTable = new HashMap<Short, Class>();
        try {
            requestTable.put(Constants.CMSG_AUTH, Class.forName("clientrequest." + "RequestLogin"));
            requestTable.put(Constants.CMSG_DISCONNECT, Class.forName("clientrequest." + "RequestLogout"));
            requestTable.put(Constants.CMSG_HEART, Class.forName("clientrequest." + "RequestHeartbeat"));
            requestTable.put(Constants.CMSG_BOARD_QUESTION, Class.forName("clientrequest." + "RequestBoardQuestion"));
            requestTable.put(Constants.CMSG_BOARD_ANSWER, Class.forName("clientrequest." + "RequestBoardAnswer"));
            requestTable.put(Constants.CMSG_QUESTION_LOG, Class.forName("clientrequest." + "RequestQuestionLog"));
            requestTable.put(Constants.CMSG_LOCAL_PLAYERS, Class.forName("clientrequest." + "RequestLocalPlayers"));
            requestTable.put(Constants.CMSG_GLOBAL_CHAT, Class.forName("clientrequest." + "RequestChatGlobal"));
            requestTable.put(Constants.CMSG_PRIVATE_CHAT, Class.forName("clientrequest." + "RequestChatPrivate"));
            requestTable.put(Constants.CMSG_GET_PRIVATE_CHAT, Class.forName("clientrequest." + "RequestUpdateChatPrivate"));
            requestTable.put(Constants.CMSG_PUBLIC_CHAT, Class.forName("clientrequest." + "RequestChatPublic"));
            requestTable.put(Constants.CMSG_BUDDIES, Class.forName("clientrequest." + "RequestBuddies"));
            requestTable.put(Constants.CMSG_BUDDY_ACCEPT, Class.forName("clientrequest." + "RequestBuddyAccept"));
            requestTable.put(Constants.CMSG_BUDDY_INVITE, Class.forName("clientrequest." + "RequestBuddyInvite"));
            requestTable.put(Constants.CMSG_BUDDY_REMOVE, Class.forName("clientrequest." + "RequestBuddyRemove"));
            requestTable.put(Constants.CMSG_GET_BUDDY_INVITES, Class.forName("clientrequest." + "RequestUpdateBuddyInvites"));

        } catch (ClassNotFoundException ex) {
            Log.e(TAG,"Class Not Found Exception in method init()"+ex.toString());
        }
    }

    public static RequestToServer get(short request_id) {
        RequestToServer request = null;
        try {
            Class name = requestTable.get(request_id);

            if (name != null) {
                request = (RequestToServer) name.newInstance();
            } else {
                System.out.println("Invalid Request Code: " + request_id);
            }
        } catch (Exception e) {
        	Log.e(TAG,"Exception in method get()"+e.toString());
        }

        return request;
    }
}